﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigUnitTypesCtrlViewModel : BaseViewModel<ConfigUnitTypesCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";

        private ObservableCollection<ConfigUnitTypesModel> _displayConfigUnitTypesModels = new ObservableCollection<ConfigUnitTypesModel>();

        #endregion

        #region Constructor
       
        public ConfigUnitTypesCtrlViewModel()
        {
        }
        
        #endregion

        #region Properties

        public ObservableCollection<ConfigUnitTypesModel> DisplayConfigUnitTypesModels
        {
            get
            {
                _displayConfigUnitTypesModels.Clear();
                List<ConfigUnitTypesModel> lst = ConfigUnitTypesDataGridViewModel.Instance.ConfigUnitTypesModel.ToList().Where(O => O.Name.Contains(_seachValue) || O.Code.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                
                _displayConfigUnitTypesModels.Add(item);
                }
                return _displayConfigUnitTypesModels;
            }

            set
            {
                _displayConfigUnitTypesModels = value;
                RaisePropertyChanged("DisplayConfigUnitTypesModels");
            }
        }


        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigUnitTypesModels");
            }
        }

        #endregion

        #region Event

        #endregion
        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigUnitTypesModelService.Instance.Update(sender as ConfigUnitTypesModel);
        }
        private void AddConfigUnitTypesModel(ConfigUnitTypesModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {
                ConfigUnitTypesModelService.Instance.Update(obj);
                ConfigUnitTypesDataGridViewModel.Instance.ConfigUnitTypesModel.Add(obj);
            }
            else ConfigUnitTypesModelService.Instance.Insert(obj);


        }

        #endregion
        #region Private Methods
        public void DelConfigUnitTypesModel(ConfigUnitTypesModel configUnitTypesModel)
        {
            if (configUnitTypesModel != null)
            {
                ConfigUnitTypesDataGridViewModel.Instance.ConfigUnitTypesModel.Remove(configUnitTypesModel);
                RaisePropertyChanged("DisplayConfigUnitTypesModels");
                ConfigUnitTypesModelService.Instance.DeleteById(configUnitTypesModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigUnitTypesModel model = new ConfigUnitTypesModel();
            DisplayConfigUnitTypesModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigUnitTypesModel(sender as ConfigUnitTypesModel);
        }

        #endregion
        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
